package com.yangli.leecode.mashib.interview;

/**
 * @Description 定义一个数的stepnum的规则，查找有没有数到stepNum
 * @Author liyang
 * @Date 2023/2/9 10:51
 */
public class Eighteen {
    public static void main(String[] args){
        Eighteen eighteen = new Eighteen();

        System.out.println(eighteen.stepNum(680));
        System.out.println(eighteen.findOriginal(753));

    }

    public int findOriginal(int stepNum){
        if (stepNum < 10) {
            return stepNum;
        }
        int l = 10;
        int r = stepNum - 1;
        while (l <= r) {//==>双指针等于的时候要带上，不然等于的时候没走循环
            int mid = (l + r) >> 1;//==>l+((r-l)>>1)这样就不会出现l+r越界的问题
            int cur = stepNum(mid);
            if (cur == stepNum) {//少算一次
                return mid;
            } else if (cur > stepNum) {
                r = mid - 1;//当前的不是就不要计算了
            } else {
                l = mid + 1;
            }
        }
        return -1;

    }

    public int stepNum(int num){
        if (num < 10) {
            return num;
        }
        int res = num;
        while (num > 9) {
            num = num / 10;
            res += num;
        }
        return res;
    }
}
